package org.pearl.springboot.minio.sdk;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.RemoveBucketArgs;
import io.minio.errors.*;
import io.minio.messages.Bucket;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * Created by TD on 2021/10/19
 */
public class MinioBukect {
    public static void main(String[] args) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        // 初始化客户端
        MinioClient minioClient =
                MinioClient.builder()
                        // minio服务端地址URL
                        .endpoint("http://localhost:9000")
                        // 用户名及密码（访问密钥/密钥）
                        .credentials("admin", "admin123")
                        .build();
        // 1.存储桶是否存在
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build());
        if (found) {
            System.out.println("my-bucketname exists");
        } else {
            System.out.println("my-bucketname does not exist");
        }

        // 2.1 my-bucketname 存储桶不存在则创建
        if (!found) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("my-bucketname").build());
            System.out.println("my-bucketname is created successfully");
        }

        // 2.2 创建存储桶my-bucketname-in-eu 并指定区域为eu-west-1
        if (!minioClient.bucketExists(
                BucketExistsArgs.builder().bucket("my-bucketname-in-eu").build())) {
            minioClient.makeBucket(
                    MakeBucketArgs.builder().bucket("my-bucketname-in-eu").region("eu-west-1").build());
            System.out.println("my-bucketname-in-eu is created successfully");
        }

        // 2.3 创建存储桶 'my-bucketname-in-eu-with-object-lock' 区域为 'eu-west-1' ，
        // 并且锁定对象，功能不可用
        if (!minioClient.bucketExists(
                BucketExistsArgs.builder().bucket("my-bucketname-in-eu-with-object-lock").build())) {
            minioClient.makeBucket(
                    MakeBucketArgs.builder()
                            .bucket("my-bucketname-in-eu-with-object-lock")
                            .region("eu-west-1")
                            .objectLock(true)
                            .build());
            System.out.println("my-bucketname-in-eu-with-object-lock is created successfully");
        }

        // 3.  存储桶信息列表。
        List<Bucket> bucketList = minioClient.listBuckets();
        for (Bucket bucket : bucketList) {
            System.out.println(bucket.creationDate() + ", " + bucket.name());
        }

        // 4. 删除存储桶
        if (found) {
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket("my-bucketname-in-eu-with-object-lock").build());
            System.out.println("my-bucketname is removed successfully");
        } else {
            System.out.println("my-bucketname does not exist");
        }

    }
}
